#pragma once
#include "xparser.h"
//#include <D3dx9anim.h>
#include <d3dx9.h>
struct stFrame :
	public D3DXFRAME
{
	stFrame()
	{

		pMeshContainer = 0; 
		pFrameSibling = 0;
		pFrameFirstChild = 0;
		Name = 0;
	}
	D3DXMATRIX conbinedTransform;
};

struct stMeshContainer :
	public D3DXMESHCONTAINER
{
	bool skinMesh;
	LPD3DXMESH pMSkinMesh;
	IDirect3DTexture9 **ppTexture;
	D3DXMATRIX **ppFrameMatrices;
	D3DXMATRIX *pBoneMatrices;
};

struct stAnimationKey0
{
	float time;
	float x, y, z, w;
};
struct stAnimationKey1
{
	float time;
	float x, y, z;
};
struct stAnimationKey2
{
	float time;
	float x, y, z;
};
struct stAnimationKey4
{
	float time;
	D3DXMATRIX matTrans;
};
struct stAnimation
{
	stAnimation()
		:name(0), pBone(0), numKey0(0), keys0(0), next(0)
		 , numKey1(0), keys1(0)
		 , numKey2(0), keys2(0)
		 , numKey4(0), keys4(0)
	{
	}
	LPSTR name;
	stFrame *pBone;
	unsigned int numKey0;
	stAnimationKey0 *keys0;
	unsigned int numKey1;
	stAnimationKey1 *keys1;
	unsigned int numKey2;
	stAnimationKey2 *keys2;
	unsigned int numKey4;
	stAnimationKey4 *keys4;
	stAnimation *next;
};

struct stAnimationSet
{
	LPSTR name;
	int length;
	unsigned int numAnimation;
	stAnimation *pAnimations;
	stAnimationSet *next;
};

class Skeleton :
	public XParser
{
public:
	Skeleton(void);
	~Skeleton(void);
	Skeleton(LPDIRECT3DDEVICE9 device):
		m_Device(device)
		{
			m_Frame = 0;
			m_MeshContainer = 0;
			m_AnimationSet = 0;
			angle = 0;
		}
	bool Rend();
	bool Adjust();
	bool Update(int time, const char * name = 0);
	stFrame* getFrame(stFrame *frame, LPCSTR name);
	void UpdateHierarchy(stFrame *frame, D3DXMATRIX *mat = NULL);
	void UpdateHierarchy(D3DXMATRIX *mat);

protected:
	bool ParseObject(ID3DXFileData *obj, ID3DXFileData *parentObj, DWORD depth, void **data, bool ref);
	//bool ParseObject(IDirectXFileData *obj, IDirectXFileData *parentObj, DWORD depth, void **data, bool ref);

private:
	stFrame *m_Frame;
	stMeshContainer *m_MeshContainer;
	stAnimationSet *m_AnimationSet;
	LPDIRECT3DDEVICE9 m_Device;
	float angle;
};
